<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="控制台测试" tabindex="-1"><a class="header-anchor" href="#控制台测试"><span>控制台测试</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#expecting-input-and-output">期望输入 / 输出</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>除了简化 HTTP 测试之外， Laravel 为测试用户输入的控制台应用提供了简单的 API 。</p>
<h2 id="期望输入-输出" tabindex="-1"><a class="header-anchor" href="#期望输入-输出"><span>期望输入 / 输出</span></a></h2>
<p>Laravel 使用 <code v-pre>expectsQuestion</code> 方法为控制台命令「模拟」用户输入。此外，还可以使用 <code v-pre>assertExitCode</code> 和 <code v-pre>expectsOutput</code> 方法指定控制台命令退出码和期望输出文本。举例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Artisan</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'question'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">ask</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$language</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">choice</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Which language do you program in?'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'PHP'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'Ruby'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'Python'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">line</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Your name is '</span><span class="token operator">.</span><span class="token variable">$name</span><span class="token operator">.</span><span class="token string single-quoted-string">' and you program in '</span><span class="token operator">.</span><span class="token variable">$language</span><span class="token operator">.</span><span class="token string single-quoted-string">'.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>您可以参考下面的示例代码来测试命令，其中使用了 <code v-pre>expectsQuestion</code> ，<code v-pre>expectsOutput</code> ，和<code v-pre>assertExitCode</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 测试控制台命令</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">test_console_command</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'question'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">expectsQuestion</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Taylor Otwell'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">expectsQuestion</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Which language do you program in?'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'PHP'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">expectsOutput</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Your name is Taylor Otwell and you program in PHP.'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">assertExitCode</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/6.x/console-tests/5184" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/con...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/6.x/console-tests/5184" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/con...</a></p>
</blockquote>
</div></template>


